﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.Basics.ModuleOpt
{
    public partial class ModuleList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        private string _s_code;
        private string _s_name;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _s_code = Request["s_code"] == null ? "" : Request["s_code"];
            _s_name = Request["s_name"] == null ? "" : Request["s_name"];

            //初始化控件值
            tb_s_code.Text = _s_code;
            tb_s_name.Text = _s_name;

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            BindList(_framePage.PageIndex, _framePage.PageSize);
        }

        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<CmsModuleData>();
                SetupCondition(ref criteria);
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        }

        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            if (!string.IsNullOrEmpty(_s_code.Trim()))
            {
                criteria.Add(Expression.Like("ModuleCode", "%" + _s_code + "%"));
            }

            if (!string.IsNullOrEmpty(_s_name.Trim()))
            {
                criteria.Add(Expression.Like("ModuleName", "%" + _s_name + "%"));
            }
        }

        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<CmsModuleData>();
                SetupCondition(ref criteria);
                object obj = criteria.SetProjection(Projections.RowCount()).UniqueResult();
                return int.Parse(obj.ToString());
            }
        }
    }
}
